package com.issi.process;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import com.iisi.logical.Logic;
import com.iisi.logical.Utils;
import com.iisi.model.PseudoDTO;

public class ReadInputData {
    private static final int amount = 100;
    private static final String menCode = "159";
    private static final String womenCode = "259";

    public static void main(String[] args) {
        List<String> inputData = Utils.inputReadFile("input001.csv");
        System.out.println("inputData size : " + inputData.size());
        ReadInputData aReadInputData = new ReadInputData();
        aReadInputData.exec(inputData);
    }

    public void exec(final List<String> originalList) {
        Logic aLogic = new Logic();
        List<PseudoDTO> tmpList = covertDTO(originalList);
        Map<String, List<PseudoDTO>> initData = calculateAmount(tmpList);
       
        for (String key : initData.keySet()) {
        	StringBuffer sbf =null;
            List<String> outPutStringList = new ArrayList<String>();
            List<PseudoDTO> processList = initData.get(key);
            final List<String> menList = aLogic.generatePseudoData(key + menCode, processList.size() * amount);
            final List<String> womenList = aLogic.generatePseudoData(key + womenCode, processList.size() * amount);
            int count = 0;
            for (PseudoDTO aPseudoDTO : processList) {
                String opid = aPseudoDTO.getId();
                
                for (int i = 0; i < amount; ++i) {
                	sbf = new StringBuffer();
                    sbf.append(menList.get(count * amount + i)).append(",\\ ,0,\\ ,\\ ,1,1,").append(opid).append(",");
                    sbf.append(opid).append(",").append(opid).append(",").append("\r");
                    outPutStringList.add(sbf.toString());
                }
                for (int i = 0; i < amount; ++i) {
                	sbf = new StringBuffer();
                    sbf.append(womenList.get(count * amount + i)).append(",\\ ,0,\\ ,\\ ,1,2,").append(opid).append(",");
                    sbf.append(opid).append(",").append(opid).append(",").append("\r");
                    outPutStringList.add(sbf.toString());
                }
                count += 1;
            }
            
            Utils.outputFile("d://" + key + ".txt", outPutStringList);
        }
    }

    private List<PseudoDTO> covertDTO(final List<String> originalList) {
        List<PseudoDTO> result = new ArrayList<PseudoDTO>();
        String patternString = ",";
        for (String aString : originalList) {
            String[] tmp = Pattern.compile(patternString).split(aString);
            List<String> aStringList = new ArrayList<String>();
            for (String atmp : tmp) {
                if (atmp.length() > 0) {
                    aStringList.add(atmp);
                }
            }
            if (aStringList.size() > 1) {
                PseudoDTO aPseudoDTO = new PseudoDTO();
                aPseudoDTO.setId(aStringList.get(0).trim());
                aPseudoDTO.setCountryName(aStringList.get(1).trim());
                result.add(aPseudoDTO);
            }

        }
        return result;
    }

    private Map<String, List<PseudoDTO>> calculateAmount(final List<PseudoDTO> originalInputList) {
        Map<String, List<PseudoDTO>> result = new HashMap<String, List<PseudoDTO>>();
        Map<String, String> referenceMap = getCountrySymbol();
        Set<String> keys = referenceMap.keySet();

        for (PseudoDTO aPseudoDTO : originalInputList) {
            String name = aPseudoDTO.getCountryName();
            for (String refName : keys) {
                if (name.contains(refName)) {
                    String symbol = referenceMap.get(refName);
                    List<PseudoDTO> initList = result.get(symbol);
                    if (initList == null) {
                        initList = new ArrayList<PseudoDTO>();
                    }
                    initList.add(aPseudoDTO);
                    result.put(symbol, initList);
                    break;
                }
            }
        }
        return result;
    }

    private static Map<String, String> getCountrySymbol() {
        Map<String, String> result = new HashMap<String, String>();
        result.put("宜蘭縣", "G");
        result.put("桃園縣", "H");
        result.put("新竹縣", "J");
        result.put("宜蘭縣", "G");
        result.put("苗栗縣", "K");
        result.put("彰化縣", "N");
        result.put("南投縣", "M");
        result.put("雲林縣", "P");
        result.put("嘉義縣", "Q");
        result.put("屏東縣", "T");
        result.put("臺東縣", "V");
        result.put("花蓮縣", "U");
        result.put("澎湖縣", "X");

        result.put("基隆市", "C");
        result.put("新竹市", "O");

        result.put("嘉義市", "I");
        result.put("連江縣", "Z");
        result.put("金門縣", "W");
        result.put("臺北市", "A");
        result.put("高雄市", "E");

        result.put("新北市", "F");
        result.put("臺中市", "B");
        result.put("臺南市", "D");

        return result;
    }
}
